package com.onlineOrder.webAction;

import com.onlineOrder.utils.jdbcDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.*;

@WebServlet("/login")
public class loginServlet extends HttpServlet {

    public static String homeName="管理员";
    public static String ID="";
    public static int count=0;  //餐品数量
    public static int cost=0;   //订餐总费用
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //验证用户名密码
        String loginName = request.getParameter("loginName");
        String loginPwd = request.getParameter("loginPwd");
        String loginFlag = request.getParameter("flag");

        String sql="select * from admin where id=? and pwd=?";
        String table="admin";

        if (loginFlag.equals("1")){
            table="user";
            sql="select userName from user where id=? and pwd=?";
        }else if (loginFlag.equals("2")){
            table="shop";
            sql="select shopName from shop where id=? and pwd=?";
        }

//        String homeName="管理员";

//        System.out.println(table);

        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null,resultSet=null;

        boolean result=false;

        try {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");//第二种
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            //获取连接
            conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineOrder","root","123456");
//            conn= jdbcDao.getConnection();

            //编译sql
            ps=conn.prepareStatement(sql);
//            ps.setString(1,table);
            ps.setString(1,loginName);
            ps.setString(2,loginPwd);

            rs=ps.executeQuery();
            if (loginFlag.equals("2")&&rs.next()){
                homeName=rs.getString("shopName");
                ID=loginName;
                result=true;
            }else if (loginFlag.equals("1")&&rs.next()){
                homeName=rs.getString("userName");
                ID=loginName;
                result=true;
            }else if (loginFlag.equals("3")&&rs.next()){
                homeName="管理员";
                result=true;
            }

//            rs=ps.executeQuery();
//            if (resultSet.next()){
//                result=true;
//            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//            jdbcDao.close(conn,ps,rs);
        }

        if (result){
            //跳转到首页
            if (table.equals("user")){
                response.sendRedirect(request.getContextPath()+"/iframeUser.jsp");
            }else if (table.equals("shop")){
                response.sendRedirect(request.getContextPath()+"/iframeShop.jsp");
            }else if (table.equals("admin")){
                response.sendRedirect(request.getContextPath()+"/iframe.jsp");
            }
        }else{
            //失败提示
            if (loginFlag.equals("1"))
                response.sendRedirect(request.getContextPath()+"/userRegister.jsp");
            else if (loginFlag.equals("2"))
                response.sendRedirect(request.getContextPath()+"/shopRegister.jsp");
            else
                response.sendRedirect(request.getContextPath()+"/error.jsp");
        }
    }
}
